package org.uim.cricket.entities.parser.impl;

import org.apache.commons.lang3.StringEscapeUtils;
import org.uim.cricket.entities.FullGround;
import org.uim.cricket.entities.parser.GroundEntityParsable;
import org.uim.cricket.entities.parser.filters.GroundDetailFilterable;
import org.uim.cricket.util.HTMLStringUtils;
import org.uim.cricket.util.MyStringUtils;

public class HTMLGroundEntityParser implements GroundEntityParsable {

	private String[] ignoreAttributes = null;
	private GroundDetailFilterable groundFilter = null;
	
	public HTMLGroundEntityParser(GroundDetailFilterable groundFilter) {
		this.groundFilter = groundFilter;
	}
	
	@Override
	public FullGround parseGround(StringBuilder source) {
		FullGround ground = new FullGround();
		
		int pos = source.indexOf("<!-- START 650 AREA -->");
		if (pos < 0) return null;
		
		String src = source.substring(pos + "<!-- START 650 AREA -->".length());
		src = src.replaceAll("&nbsp;", "");
		
		String headerName = MyStringUtils.inBetween(src, "<h1>", "</h1>");
		if (headerName == null) return null;
		
		headerName = StringEscapeUtils.unescapeHtml4(headerName);
		headerName = HTMLStringUtils.cleanHTMLTags(headerName);
		
		ground.setFullName(headerName.trim());
		
		String loc = MyStringUtils.inBetween(src, "<p class=\"loc\">", "</p>");
		if (loc != null) {
			loc = HTMLStringUtils.cleanHTMLTags(loc);
			loc = StringEscapeUtils.unescapeHtml4(loc);
			
			pos = loc.lastIndexOf(',');
			if (pos >= 0) {
				ground.setCountry(loc.substring(pos + 1).trim());
				int pos2 = loc.lastIndexOf(',', pos - 1);
				if (pos > 0) {
					ground.setTown(loc.substring(pos2+1, pos).trim());
				} else {
					ground.setTown(loc.substring(0, pos).trim());
				}
			} 
		}
		String address = MyStringUtils.inBetween(src, "<address>", "</address>");
		if (address != null) {
			address = HTMLStringUtils.cleanHTMLTags(address);
			address = StringEscapeUtils.unescapeHtml4(address);
			ground.addNewAttribute("Address", address);
		}
		
		String data = MyStringUtils.inBetween(src, "<div id=\"stats\"", "</div>");
		if (data == null) return ground;
		pos = data.indexOf('>');
		if (pos < 0) return ground;
		data = data.substring(pos+1);
		
		pos = data.indexOf("<br/>");
		int spos = 0;
		while (pos >= spos) {
			String attrib = data.substring(spos, pos);
			String[] res = this.breakIntoParts(attrib);
			spos = pos + "<br/>".length();
			
			if (res != null) {
				
				boolean toadd = false;
				if (this.ignoreAttributes != null) {
					if (!this.containInIgnoreList(res[0])) {
						toadd = true;
					}
				} else {
					toadd = true;
				}
				
				if (toadd) {
					if (this.groundFilter != null) {
						this.groundFilter.filterGroundAttribute(ground, res[0], res[1]);
					} else {
						ground.addNewAttribute(res[0], res[1]);
					}
				}
				
			}
			
			pos = data.indexOf("<br/>", spos);
		}
		
		return ground;
	}

	private boolean containInIgnoreList(String key) {
		for (int i=0;i<this.ignoreAttributes.length;i++) {
			if (this.ignoreAttributes[i].startsWith(key)) return true;
		}
		return false;
	}
	
	private String[] breakIntoParts(String attrib) {
		int pos = attrib.indexOf("</label>");
		if (pos < 0) return null;
		String[] res = new String[2];
		res[0] = HTMLStringUtils.cleanHTMLTags(attrib.substring(0, pos).trim());
		res[1] = attrib.substring(pos + "</label>".length()).trim();
		return res;
	}
	
	public void setGroundFilter(GroundDetailFilterable groundFilter) {
		this.groundFilter = groundFilter;
	}
	
	public GroundDetailFilterable getGroundFilter() {
		return groundFilter;
	}
	
	public void setIgnoreAttributes(String[] ignoreAttributes) {
		this.ignoreAttributes = ignoreAttributes;
	}
	
	public String[] getIgnoreAttributes() {
		return ignoreAttributes;
	}
	
	
}
